package com.dounion.sample.utils;

import cn.hutool.http.HttpRequest;
import com.dounion.framework.entity.Model;
import com.dounion.framework.helper.AssertHelper;
import com.dounion.framework.helper.ConfigHelper;
import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.servlet.ModelAndView;

import javax.swing.*;
import java.net.HttpCookie;
import java.util.List;

import static com.dounion.sample.common.Constant.*;

/**
 * 用于获取每日涨幅等常用接口
 */
@Slf4j
public class StockUtils {

    public static List loadStockDaily(Model input){
        HttpCookie cookie=new HttpCookie("xq_a_token", ConfigHelper.getString("xueqiu.token",XQ_A_TOKEN));
        String body = HttpRequest.get(STOCK_DAILY_URL)
                .cookie(cookie)
                .form(input)
                .execute()
                .body();
        Model model = Model.parseObject(body);
        AssertHelper.EX_VALIDATION.isEquals(model, "error_code", 0, "loadStockDaily.dataAbnormally");
        return model.getModel("data", true).getList("item");
    }

    /**
     * 获取当前价格
     * @param symbol
     * @return
     */
    public static Model pankou(String symbol){
        Model query = Model.builder();
        query.set("symbol",symbol);
        HttpCookie cookie=new HttpCookie("xq_a_token", ConfigHelper.getString("xueqiu.token",XQ_A_TOKEN));
        String body = HttpRequest.get(STOCK_PAN_KOU)
                .cookie(cookie)
                .form(query)
                .execute()
                .body();
        Model model = Model.parseObject(body);
        AssertHelper.EX_VALIDATION.isEquals(model, "error_code", 0, "loadStockDaily.dataAbnormally");
        return model.getModel("data", true);
    }

//    public static void main(String[] args) {
//        Model input = Model.builder();
//        input.set("symbol", "SH510300");
//        input.set("begin", "1627951939301");
//        input.set("period", "day");
//        input.set("type", "before");
//        input.set("count", "-284");
//        input.set("indicator", "kline,pe,pb,ps,pcf,market_capital,agt,ggt,balance");
//        return loadStockDaily(input);
//    }
}
